<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller\api\permissions;

use App\Common\Repositories\System\CustomerAdminRepository;
use App\Common\Repositories\System\CustomerLogLoginRepository;
use App\Common\Repositories\System\CustomerLogOperateRepository;
use xsl\basic\BaseController;

class Admin extends BaseController
{
    protected $repository;

    public function __construct(CustomerAdminRepository $repository)
    {
        $this->repository = $repository;
    }

    public function online()
    {
        if ($this->request->isMethod('GET')) {
            $user = $this->repository->permissions($this->request->adminId());
            $user->from = 'think';
            return $this->success($user);
        }

        $params = $this->request->all();
        if(!empty($params['password'])){
            $params['password'] = password_hash($params['password'], PASSWORD_DEFAULT);
        }
        $this->repository->update($this->request->adminId(), $params);
        return $this->success();
    }


    public function loginLog(){
        $params = $this->request->all();
        $page = $params['page'] ?? 1;
        $limit = $params['limit'] ?? 10;
        $customerLogLoginRepository = \Hyperf\Support\make(CustomerLogLoginRepository::class);
        $admin = $this->request->adminInfo();
        $where['account'] = $admin->account;
        $result = $customerLogLoginRepository->getUserLogBy($where, intval($limit));
        return $this->pageSuccess($limit, $result->total(), $result->items());
    }

    public function operateLog(){
        $scope = $this->request->input('scope', 'self');
        $page = $this->request->input('page', 1);
        $limit = $this->request->input('limit', 10);
        $customerLogOperateRepository = \Hyperf\Support\make(CustomerLogOperateRepository::class);
        $where = [];
        if ($scope == 'self') {
            $where['creator_id'] = $this->request->adminId();
        }
        $result = $customerLogOperateRepository->getWhereList($where, intval($limit));
        return $this->pageSuccess($limit, $result->total(), $result->items());
    }
}
